package com.oig.sys.workflow.controller;


import com.oig.common.exception.BadRequestException;
import com.oig.common.module.SysRequest;
import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.log.annotation.SysLog;
import com.oig.sys.security.util.SecurityUtil;
import com.oig.sys.workflow.dto.ListenerObjectDto;
import com.oig.sys.workflow.service.ITbListenerObjectService;
import com.oig.sys.workflow.vo.ListenerObjectVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 监听对象管理
 */
@Tag(name = "监听对象管理")
@RestController
@RequestMapping("/listenerObject")
@Slf4j
@RequiredArgsConstructor
public class TbListenerObjectController {

    private final ITbListenerObjectService listenerObjectService;


    /**
     * 配置需要复核才能生效
     */
    @PreAuthorize("@pms.hasPermission('/listenerObject/add')")
    @SysLog(value="添加监听对象",apiUrl="/listenerObject/add")
    @Operation(summary = "添加监听对象")
    @PostMapping(value = "/add")
    public ListenerObjectVo addListenerObject(@RequestBody @Validated(GroupAdd.class) ListenerObjectDto listenerObjectDto){
        log.info("add listener object, {}", listenerObjectDto);
        if (SecurityUtil.getDefineUserMain().getTenantId()!=0){
            throw new BadRequestException("租户无权操作");
        }
        listenerObjectDto.setCreateUserId(SecurityUtil.getDefineUserMain().getId());
        listenerObjectDto.setCreateUserName(SecurityUtil.getDefineUserMain().getRealName());
        listenerObjectDto.setCreateTime(new Date());
        listenerObjectDto.setLastUpdateId(SecurityUtil.getDefineUserMain().getId());
        listenerObjectDto.setLastUpdateName(SecurityUtil.getDefineUserMain().getRealName());
        listenerObjectDto.setLastUpdateTime(new Date());
        return this.listenerObjectService.addListenerObject(listenerObjectDto);
    }


    @PreAuthorize("@pms.hasPermission('/listenerObject/update')")
    @SysLog(value="修改监听对象",apiUrl="/listenerObject/update")
    @Operation(summary = "修改监听对象")
    @PostMapping(value = "/update")
    public ListenerObjectVo updateListenerObject(@RequestBody @Validated(GroupEdit.class) ListenerObjectDto listenerObjectDto){
        log.info("update listener object, {}", listenerObjectDto);
        if (SecurityUtil.getDefineUserMain().getTenantId()!=0){
            throw new BadRequestException("租户无权操作");
        }
        listenerObjectDto.setLastUpdateId(SecurityUtil.getDefineUserMain().getId());
        listenerObjectDto.setLastUpdateName(SecurityUtil.getDefineUserMain().getRealName());
        listenerObjectDto.setLastUpdateTime(new Date());
        return this.listenerObjectService.updateListenerObject(listenerObjectDto);
    }



    @PreAuthorize("@pms.hasPermission('/listenerObject/audit')")
    @SysLog(value="复核",apiUrl="/listenerObject/audit")
    @Operation(summary = "复核")
    @PostMapping(value = "/audit")
    public ListenerObjectVo auditListenerObject(@RequestParam("objectId")Integer objectId,
                                                @RequestParam("resultType")String resultType,
                                  @RequestParam("remark")String remark){
        log.info("audit listener object:{}-{}-{}", objectId, resultType, remark);
        if (SecurityUtil.getDefineUserMain().getTenantId()!=0){
            throw new BadRequestException("租户无权操作");
        }
        ListenerObjectDto listenerObjectDto = new ListenerObjectDto();
        listenerObjectDto.setId(objectId);
        listenerObjectDto.setReqType(resultType);
        listenerObjectDto.setCheckRemark(remark);
        listenerObjectDto.setCheckId(SecurityUtil.getDefineUserMain().getId());
        listenerObjectDto.setCheckName(SecurityUtil.getDefineUserMain().getRealName());
        listenerObjectDto.setCheckTime(new Date());
        return this.listenerObjectService.auditListenerObject(listenerObjectDto);
    }


    @PreAuthorize("@pms.hasPermission('/listenerObject/cancel')")
    @SysLog(value="逻辑删除监听对象",apiUrl="/listenerObject/cancel")
    @Operation(summary = "逻辑删除监听对象")
    @PostMapping(value = "/cancel/{objectId}")
    public SysResponse<Integer> cancelListenerObject(@PathVariable("objectId") Integer objectId){
        log.info("cancel listener object id={}", objectId) ;
        if (SecurityUtil.getDefineUserMain().getTenantId()!=0){
            throw new BadRequestException("租户无权操作");
        }
        this.listenerObjectService.cancelListenerObject(objectId);
        return SysResponse.ok(objectId,"逻辑删除监听对象成功");
    }


    @PreAuthorize("@pms.hasPermission('/listenerObject/del')")
    @SysLog(value="物理删除监听对象",apiUrl="/listenerObject/del")
    @Operation(summary = "物理删除监听对象")
    @PostMapping(value = "/del/{objectId}")
    public SysResponse<Integer> delListenerObject(@PathVariable("objectId") Integer objectId){
        log.info("del listener object id={}", objectId) ;
        if (SecurityUtil.getDefineUserMain().getTenantId()!=0){
            throw new BadRequestException("租户无权操作");
        }
        this.listenerObjectService.delListenerObject(objectId);
        return SysResponse.ok(objectId,"物理删除监听对象成功");
    }

    @Operation(summary = "查看监听对像详细信息")
    @PostMapping(value = "/info/{objectId}")
    public ListenerObjectVo getListenerObject(@PathVariable("objectId") Integer objectId){
        log.info(" get listener object id=:{}", objectId);
        return this.listenerObjectService.getListenerObject(objectId);
    }

    @Operation(summary = "查看监听对像列表,提供备选")
    @PostMapping("/list")
    public List<ListenerObjectVo> list(@RequestBody SysRequest<ListenerObjectDto> ListenerObjRequest){
        log.info("list listener object :{}",ListenerObjRequest);
        return this.listenerObjectService.queryList(ListenerObjRequest);
    }

}
